using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using CSW.Framework.Common.Binding;
using CSW.Security.UI.DevEx.Forms;
using DevExpress.XtraEditors;
using CSW.Framework.UI.DevEx;
using CSW.Security;
using CSW.Security.UI.DevEx.Search;
using System.Collections.Generic;
using CSW.Framework.Common;

namespace CSW.Security.UI.DevEx.UserControls
{
    /// <summary>
    /// SecurityAppFunctionListUserControl
    /// </summary>
    public partial class SecurityAppFunctionListUserControl : XtraUserControl
    {
        private readonly EditGridController<Security_AppFunction_Search, Security_AppFunction> m_Grid;

        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityAppFunctionListUserControl"/> class.
        /// </summary>
        public SecurityAppFunctionListUserControl()
        {
            InitializeComponent();

            m_Grid = new EditGridController<Security_AppFunction_Search, Security_AppFunction>(baseGridControl1);
            m_Grid.AddField("ID", "AppFunctionID");
            m_Grid.AddField("Description", "Description");
            m_Grid.AddEditButtons();
            m_Grid.NewRecord += new CSW.Framework.UI.Binding.NewRecordEventHandler<Security_AppFunction>(m_Grid_NewRecord);
            m_Grid.EditRecord += new CSW.Framework.UI.Binding.EditRecordEventHandler<Security_AppFunction>(m_Grid_EditRecord);
            m_Grid.DeleteRecord += new EventHandler<CSW.Framework.Common.DataEventArgs<System.Collections.Generic.IList<Security_AppFunction_Search>>>(m_Grid_DeleteRecord);

            RefreshList();
        }

        private void m_Grid_DeleteRecord(object sender, DataEventArgs<IList<Security_AppFunction_Search>> args)
        {
            IList<Security_AppFunction_Search> list = args.Data;
            if (list != null && list.Count > 0)
            {
                DialogResult res = MessageBox.Show("Delete selected record(s)?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    Cursor.Current = Cursors.WaitCursor;

                    foreach (var appFunction in list.Select(p => p.GetBusinessObject()))
                    {
                        if (appFunction.Security_RoleAppFunction.Count > 0)
                            MessageBox.Show(string.Format("Cannot delete app function '{0}' because it is referenced by a role.", appFunction.Description), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                            SecurityTest.DataContextInstance.Security_AppFunction.DeleteOnSubmit(appFunction);
                    }

                    SecurityTest.DataContextInstance.SubmitChanges();

                    RefreshList();

                    Cursor.Current = Cursors.Default;
                }
            }
        }

        private void m_Grid_EditRecord(object sender, CSW.Framework.UI.Binding.EditRecordEventArgs<Security_AppFunction> args)
        {
            Cursor.Current = Cursors.WaitCursor;
            using (SecurityAppFunctionEditForm form = new SecurityAppFunctionEditForm(args.Item))
            {
                Cursor.Current = Cursors.Default;
                form.ShowDialog();
            }
        }

        private void m_Grid_NewRecord(object sender, CSW.Framework.UI.Binding.NewRecordEventArgs<Security_AppFunction> args)
        {
            Cursor.Current = Cursors.WaitCursor;
            using (SecurityAppFunctionEditForm form = new SecurityAppFunctionEditForm(args.Item))
            {
                Cursor.Current = Cursors.Default;
                form.ShowDialog();
                if (args.Item.IsNew)
                    args.Remove = true;
            }
        }

        /// <summary>
        /// Refreshes the list.
        /// </summary>
        public void RefreshList()
        {
            if (SecurityTest.DataContextInstance == null)
                return;

            m_Grid.DataSource = new BindingList<Security_AppFunction_Search>(Security_AppFunction_Search.GetAll().ToList());

            m_Grid.BestFitColumns();
        }
    }
}
